package com.gitee.wsl.struct.either

import arrow.core.Either

@Suppress("NOTHING_TO_INLINE")
inline fun <X, Y> Pair<X?, Y?>.toEither(): Either<X, Y> = Either.of(first, second)

fun <X, Y> Either.Companion.of(left: X?, right: Y?): Either<X, Y> {
    if (left != null) {
        return Either.Left(left)
    }
    if (right != null) {
        return Either.Right(right)
    }
    throw IllegalArgumentException("first & second == null; At least one element must be non-null")
}

fun <A> A.toLeft(): Either<A, Nothing> = Either.Left(this)

fun <A> A.toRight(): Either<Nothing, A> = Either.Right(this)

operator fun <A : Comparable<A>, B : Comparable<B>> Either<A, B>.compareTo(other: Either<A, B>): Int =
    fold(
        { a1 -> other.fold({ a2 -> a1.compareTo(a2) }, { -1 }) },
        { b1 -> other.fold({ 1 }, { b2 -> b1.compareTo(b2) }) },
    )

